package com.ball.dev.service.impl;


import com.ball.common.dtos.ResponseResult;
import com.ball.common.enums.AppHttpCodeEnum;
import com.ball.common.utils.UserContext;
import com.ball.dev.entity.po.AppUser;
import com.ball.dev.entity.po.Personal;
import com.ball.dev.mapper.AppUserMapper;
import com.ball.dev.mapper.PersonalMapper;
import com.ball.dev.service.IAppUserService;
import com.ball.dev.service.IPersonalService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 个人信息表 服务实现类
 * </p>
 *
 * @author lou
 * @since 2023-12-04
 */
@Service
@RequiredArgsConstructor
public class PersonalServiceImpl extends ServiceImpl<PersonalMapper, Personal> implements IPersonalService {

    @Override
    public ResponseResult addPersonal(Personal personal) {
        //#补全添加的代码
        if (personal == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"添加失败");
        }

        personal.setCreateTime(LocalDateTime.now());
        personal.setUpdateTime(LocalDateTime.now());

       // 判断personal是否为空，不为空则调用save方法，返回ResponseResult对象，如果为空则返回ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"添加失败")
 return save(personal) ? ResponseResult.okResult(personal.getId()) : ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"添加失败");
    }

    @Override
    public ResponseResult getPersonalById(Long id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(),"查询失败");
        }

        Personal personal = lambdaQuery().eq(Personal::getUserId, id).one();

        if (personal == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(),"查询失败");
        }

        return ResponseResult.okResult(personal);
    }

    @Override
    public ResponseResult updatePersonal(Personal personal) {
        if (personal == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(),"更新失败");
        }

        Personal one = lambdaQuery().eq(Personal::getUserId, personal.getUserId()).one();
        if (one == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(),"更新失败");
        }
        personal.setId(one.getId());
        personal.setUpdateTime(LocalDateTime.now());

        return updateById(personal) ? ResponseResult.okResult("更新成功"):
                ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(),"更新失败");
    }

    @Override
   //获取用户列表
 public ResponseResult getUserList() {
        //使用lambdaQuery查询Personal表，并按照创建时间降序排列
        List<Personal> personals = lambdaQuery().orderByDesc(Personal::getCreateTime).list();
        //返回查询结果
        return ResponseResult.okResult(personals);
    }

    private final AppUserMapper appUserMapper;
    @Override//管理更新数据
    public ResponseResult ManageUpdate(Personal personal) {
        String userId = UserContext.getUser();
        AppUser user = appUserMapper.selectById(userId);
        if (user == null || user.getManageStatus() != 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_ADMIND);
        }
        if (personal == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(),"更新失败");
        }

        return updateById(personal) ? ResponseResult.okResult("更新成功"
        ):ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(),"更新失败");
    }

    @Override
   public ResponseResult deleteUser(String id,Integer status) {
        //获取当前用户id
        String userId = UserContext.getUser();
        //根据当前用户id查询当前用户
        AppUser user = appUserMapper.selectById(userId);
        //判断当前用户是否存在
        if (user == null || user.getManageStatus() != 1){
            //如果不存在，则返回需要管理员权限
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_ADMIND);
        }
        //判断id和status是否为空
        if (id == null || status == null){
            //如果为空，则返回数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //根据id查询Personal
        Personal personal = getById(id);
        //判断Personal是否存在
        if (personal == null){
            //如果不存在，则返回数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断Personal的userId是否为空
        if (personal.getUserId() !=null){

            //如果不为空，则将userId的status设置为1
            appUserMapper.update(Wrappers.<AppUser>lambdaUpdate()
                    .eq(AppUser::getId,personal.getUserId())
                    .set(AppUser::getStatus,status)
                    .set(AppUser::getUpdateTime,LocalDateTime.now())
            );
        }

        //根据id和status更新Personal
        return  lambdaUpdate().eq(Personal::getId,id)
                .set(Personal::getStatus,status)
                .set(Personal::getUpdateTime,LocalDateTime.now())
                .update() ?
                //如果更新成功，则返回操作成功
                ResponseResult.okResult("操作成功")
                //如果更新失败，则返回操作失败
                :ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST.getCode(), "操作失败");
    }
}
